<template>
  <div>
    <!-- 面包屑区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">
        首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>商品列表
      </el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query"
            @clear="getGoodsList" clearable>
            <el-button slot="append" icon="el-icon-search"
              @click="handleCurrentChange(1),getGoodsList">
            </el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="goAddGoodPage">
            添加商品
          </el-button>
        </el-col>
      </el-row>
      <!-- teble表格区域 -->
      <el-table :data="goodsList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name">
        </el-table-column>
        <el-table-column label="商品价格（元）" prop="goods_price"
          width="95px">
        </el-table-column>
        <el-table-column label="商品重量" prop="goods_weight"
          width="70px">
        </el-table-column>
        <el-table-column label="创建时间" prop="add_time" width="140px">
          <template v-slot="scope">
            {{scope.row.add_time | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="190px">
          <template v-slot="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini"
              @click="showeditDialog(scope.row.goods_id)">
              编辑
            </el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini"
              @click="removeById(scope.row.goods_id)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum" :page-sizes="[5, 8, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total" background>
      </el-pagination>
    </el-card>
    <!-- 编辑商品信息对话框 -->
    <el-dialog title="编辑商品信息" :visible.sync="editDialogVisibale"
      width="50%">
      <el-form :model="editForm" :rules="editFormRules"
        ref="editFormRef" label-width="100px">
        <el-form-item label="商品名称" prop="goods_name">
          <el-input v-model="editForm.goods_name"></el-input>
        </el-form-item>
        <el-form-item label="商品价格" prop="goods_price">
          <el-input v-model="editForm.goods_price"></el-input>
        </el-form-item>
        <el-form-item label="商品重量" prop="goods_weight">
          <el-input v-model="editForm.goods_weight"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisibale = false">取 消</el-button>
        <el-button type="primary" @click="editById">
          确 定
        </el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      goodsList: [],
      // 查询参数配置
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 数据总量
      total: 0,
      editDialogVisibale: false,
      editForm: {
        goods_name: '',
        goods_price: 0,
        goods_weight: 0,
        add_time: 0
      },
      editFormRules: {
        goods_name: [
          {
            required: true,
            message: '请输入分类名称',
            trigger: 'blur'
          }
        ],
        goods_price: [
          {
            required: true,
            message: '请输入商品价格',
            trigger: 'blur'
          }
        ],
        goods_weight: [
          {
            required: true,
            message: '请输入商品重量',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    this.getGoodsList()
  },
  methods: {
    //   根据分页获取相应的商品列表
    async getGoodsList() {
      const { data: res } = await this.$http.get('goods', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取商品列表失败，请稍后再试')
      }
      this.goodsList = res.data.goods
      this.total = res.data.total
    },
    // 监听每页数据大小的变化
    handleSizeChange(newsize) {
      this.queryInfo.pagesize = newsize
      this.getGoodsList()
    },
    // 监听当前页码的变化
    handleCurrentChange(newpage) {
      this.queryInfo.pagenum = newpage
      this.getGoodsList()
    },
    async showeditDialog(id) {
      const { data: res } = await this.$http.get('goods/' + id)
      if (res.meta.status !== 200) {
        return this.$message.error('查询商品信息失败，请稍后再试')
      }
      this.editForm = res.data
      this.editDialogVisibale = true
    },
    editById() {
      this.$refs.editFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.put('goods/' + this.editForm.goods_id, {
          goods_name: this.editForm.goods_name,
          goods_price: this.editForm.goods_price,
          goods_number: this.editForm.goods_number,
          goods_weight: this.editForm.goods_weight,
          goods_cat: this.editForm.goods_cat
        })
        if (res.meta.status !== 200) {
          return this.$message.error('编辑商品信息失败，请稍后再试')
        }
        this.getGoodsList()
        this.editDialogVisibale = false
        this.$message.success('编辑商品信息成功！')
      })
    },
    // 根据id删除相应的数据
    async removeById(id) {
      const confirmRes = await this.$confirm('此操作将永久删除该商品数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmRes !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      const { data: res } = await this.$http.delete(`goods/${id}`)
      if (res.meta.status !== 200) {
        return this.$message.error('删除商品数据失败，请稍后再试')
      }
      this.$message.success('删除商品数据成功！')
      this.getGoodsList()
    },
    // 点击跳转到添加商品数据页
    goAddGoodPage() {
      this.$router.push('/goods/add')
    }
  }
}
</script>
<style lang="less" scoped>
</style>